package com.app.service.impl;

import com.app.entity.Evaluate;
import com.app.entity.User;
import com.app.mapper.EvaluateMapper;
import com.app.mapper.UserMapper;
import com.app.service.EvaluateService;
import com.app.util.Result;
import com.app.util.RoleUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.annotation.ElementType;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2021-04-07
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {

    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result findEvaluateAll(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (id != null) {
            queryWrapper.eq("coaid", id);
        }
        queryWrapper.orderByDesc("create_time");
        List<Evaluate> list = evaluateMapper.selectList(queryWrapper);
        list.forEach(item -> {
            User user = userMapper.selectById(item.getCoaid());
            item.setUser(user);
        });
        return Result.success(list);
    }

    @Override
    public Result insertEvaluate(Evaluate evaluate) {
        evaluate.setCreateTime(new Date());
        evaluateMapper.insert(evaluate);
        return Result.success();
    }

    @Override
    public Result evaluateByCoach() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("coaid", RoleUtil.getRoleId());
        queryWrapper.orderByDesc("create_time");
        return Result.success(evaluateMapper.selectList(queryWrapper));
    }
}
